import defaultSettings from '@/settings'

const { showSettings, tagsView, fixedHeader, sidebarLogo, primaryColor, layoutMode, theme } =
  defaultSettings

const state = {
  theme,
  tagsView,
  layoutMode,
  fixedHeader,
  sidebarLogo,
  primaryColor,
  showSettings
}

const mutations = {
  SAVE_SETTING: (state, setting) => {
    localStorage.setItem(
      'sys_setting',
      JSON.stringify(Object.assign(defaultSettings, { ...setting }))
    )
  },
  CHANGE_SETTING: (state, { key, value }) => {
    if (state.hasOwnProperty(key)) {
      state[key] = value
      // 存本地
      localStorage.setItem(
        'sys_setting',
        JSON.stringify(Object.assign(defaultSettings, { [key]: value }))
      )
    }
  }
}

const actions = {
  saveSetting({ commit }, data) {
    commit('SAVE_SETTING', data)
  },
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
